package ro.msg.office.beans;

import java.io.IOException;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import ro.msg.office.ticketmanager.DO.UserDO;
import ro.msg.office.ticketmanager.back.ifc.UserManager;

/**
 *  This class is handling the properties from the login.xhtml file 
 * Here are the methods required for the login operation
 * @author Denisa Sulea
 *
 */
public class LoginBean {
	private String username;
	private String password;
	private String name;
	private Long userType;
	private static UserDO userDO;
	private static boolean signed = false;
	private String isSigned;
	private String isSignedAsUser;
	private String isSignedAsAdmin;
	
	@EJB
	UserManager userEJB;
	
	/**
	 *  Method used to get the user who is currently logged
	 * @return - the value of the user
	 */
	public static UserDO getUserDO() {
		return userDO;
	}

	/**
	 * Method used to set the value of the user that is successfully logged
	 * @param userDO - the new user
	 */
	public static void setUserDO(UserDO userDO) {
		LoginBean.userDO = userDO;
	}

	/**
	 * Method used to get the Username property of the user who is currently logged
	 * @return - the value of the Username property
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Method used to set the Username of the user that is logged
	 * @param username - the new value for username
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * Method used to get the Password property of the user who is currently logged
	 * @return - the value of the Password property
	 */
	public String getPassword() {
		return password;
	}

	/**
	 *  Method used to set the Password of the user that is logged
	 * @param password - the new value for password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Method used to get the Name property of the user who is currently logged
	 * @return - the value of the Name property
	 */
	public String getName() {
		return name;
	}

	/**
	 * Method used to set the Name of the user that is logged
	 * @param name - the new value for Name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 *  Method used to get the UserType property of the user who is currently logged
	 * @return - the value of the UserType property (1 for regular user, 2 for the administrator)
	 */
	public Long getUserType() {
		return userType;
	}

	/**
	 *  Method used to set the UserType of the user that is logged
	 * @param userType - the new value for userType (1 for regular user, 2 for the administrator)
	 */
	public void setUserType(Long userType) {
		this.userType = userType;
	}

	/**
	 * Method used to update the value of the user that is logged and the signed property 
	 * (tell the system that somebody is logged)
	 * @return - a String that represents a navigation between pages : if there is no error
	 * - goes to the User page if the user logged is registered as a regular user, 
	 * - or to the Administrator page if it is an administrator;
	 * both pages contain the operations that the current user is allowed to perform according to his type
	 * if there is an error, a message will be displayed on the current page
	 */
	public String signIn() {
		userDO = new UserDO();
		userDO.setUsername(username);
		userDO.setPassword(password);
		userDO = userEJB.check(userDO);
		if (userDO.getId() == null) {
			return errorLogIn();
		} else {
			this.name = userDO.getName();
			this.userType = userDO.getUserType();
			signed = true;
			if (userType == 1)
				return loginAsUser();
			else
				return loginAsAdmin();
		}
	}

	/**
	 * Method used to get the user who is currently logged
	 * @return - the value of the user
	 */
	public static UserDO getUser() {
		return userDO;
	}

	/**
	 * Method used if an error occurred during login operation
	 * @return - a String that represents a navigation between pages
	 */
	private String errorLogIn() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage fm = new FacesMessage("Username or password incorrect!");
		facesContext.addMessage(null, fm);
		return "errorLogIn";
	}

	/**
	 * Method used if an the logged user is registered as a regular user
	 * @return - a String that represents a navigation between pages
	 */
	private String loginAsUser() {
		return "loginUser";
	}

	/**
	 *  Method used if an the logged user is registered as an administrator
	 * @return - a String that represents a navigation between pages
	 */
	private String loginAsAdmin() {
		return "loginAdmin";
	}

	/**
	 * Method used to set the signed property.
	 * This property is initialized with false, because at the beginning nobody is logged, 
	 * during the signIn method signed property becomes true, and when the signOut method 
	 * is invoked the signed property is changed to false.
	 * @param signed - the new value for the signed property
	 */
	public void setSigned(boolean signed) {
		LoginBean.signed = signed;
	}

	/**
	 * Method used to get the signed property of the login page 
	 * (every time a page is loaded, for security, it is checked if the user is allowed to view that page)
	 * @return - the value of the signed property (true if there is someone logged, else false)
	 */
	public static boolean getSigned() {
		return signed;
	}
	
	/**
	 * Method used to update the value of the signed property 
	 * @return - a String that represents a navigation between pages (goes to the login page)
	 */
	public String signOut(){
		signed = false;
		return "signOut";
	}

	/**
	 * Method used to set the isSigned property (the same as the signed property but as a string)
	 * @param isSigned - the new value for the isSigned property
	 */
	public void setIsSigned(String isSigned) {
		this.isSigned = isSigned;
	}

	/**
	 * Method used to get the isSigned property of the login page 
	 * (every time the login page is loaded, for security, it is checked if there is someone logged)
	 * @return - the value of the isSigned property (nothing if nobody is logged, else a redirection to the
	 * administrator page, if the user is an administrator, or to the user page if it is a regular user)
	 */
	public String getIsSigned() {
		if(signed==true)
		{
			if(userType == 1)
				try {
					FacesContext.getCurrentInstance().getExternalContext().redirect("user.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
			else
				try {
					FacesContext.getCurrentInstance().getExternalContext().redirect("admin.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return "";
	}
	

	/**
	 * Method used to get the isSignedAsUser property of the login page 
	 * (every time a page with access only to users is loaded, for security, it is checked if the logged user has access to that page)
	 * @return - the value of the isSignedAsUser property (redirect to login page if nobody is logged, 
	 * a redirection to the administrator page, if the user is an administrator, 
	 * and nothing if it is a regular user)
	 */
	public String getIsSignedAsUser() {
		if(signed==true)
			if(userType == 2)
				try {
					FacesContext.getCurrentInstance().getExternalContext().redirect("admin.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
			else 
				return "";
		else
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
			} catch (IOException e) {
				e.printStackTrace();
			}
		return "";
	}

	/**
	 * Method used to set the isSignedAsUser property as a string
	 * @param isSignedAsUser - the new value for the isSignedAsUser property
	 */
	public void setIsSignedAsUser(String isSignedAsUser) {
		this.isSignedAsUser = isSignedAsUser;
	}

	/**
	 * Method used to get the isSignedAsAdmin property of the login page 
	 * (every time a page with access only to administrators is loaded, for security, it is checked if the logged user has access to that page)
	 * @return - the value of the isSignedAsAdmin property (redirect to login page if nobody is logged, 
	 * a redirection to the user page, if the user is a regular user, 
	 * and nothing if it is an administrator)
	 */
	public String getIsSignedAsAdmin() {
		if(signed==true)
			if(userType == 1)
				try {
					FacesContext.getCurrentInstance().getExternalContext().redirect("user.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
			else 
				return "";
		else
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
			} catch (IOException e) {
				e.printStackTrace();
			}
		return "";
	}

	/**
	 * Method used to set the isSignedAsAdmin property as a string
	 * @param isSignedAsAdmin - the new value for the isSignedAsAdmin property
	 */
	public void setIsSignedAsAdmin(String isSignedAsAdmin) {
		this.isSignedAsAdmin = isSignedAsAdmin;
	}

}
